#define __PARSE__
#ifndef __PARSE__

#include "system_data.h"
#include "stdio.h"
#include "btree.h"
#include "database.h"
#include "utility.h"
#include "malloc.h"
#include "table.h"

#define  TEST_SIZE 10
#define  __DEBUG__


int main(){
  	table* p_table;
  	DECLARE_LISTP(column) p_property;
  	column c_column;
  	Error_code error_code;	
  	tuple_t tuple;
  	char *p_table_name = "Student";
  	int i;
  	char *p_id = "00";
  	char *p_name = "name";
  	query_return_value rt;
  	data_t new_entry;
  
 	column *p_new_column;
 
  	p_property = MALLOC_A_LIST(column);
 
  	INIT_LISTP(p_property);
  	c_column.allow_null = FALSE;
  	strcpy(c_column.column_name, "ID");
  	c_column.i_column_size = 4;
  	c_column.is_key = TRUE;
  	c_column.type = INT_EY;
  	INSERTP(p_property, column, 0, c_column, error_code);
  	if(error_code != ERROR_CODE_SUCCESS)
  	{
  		printf("something wrong in the insert column in main.c at line %d .\n", __LINE__);
  		return NULL;
  	}
  	c_column.allow_null = FALSE;
  	strcpy(c_column.column_name, "NAME");
  	c_column.i_column_size = 20;
  	c_column.is_key = FALSE;
  	c_column.type = STRING_EY;
  	INSERTP(p_property, column, 1, c_column, error_code);
  	if(error_code != ERROR_CODE_SUCCESS)
  	{
  		printf("something wrong in the  insert column in main.c at line %d .\n", __LINE__);
  		return NULL;
  	}
  	
  	error_code = create_table(&p_table ,p_table_name, p_property);
  	if(error_code != ERROR_CODE_SUCCESS)
  	{
  		printf("something wrong in the  create table in main.c at line %d .\n", __LINE__);
  		return -1;
  	}
  	
  	INIT_LIST(tuple.data_list);
  	
  	for(i = 0; i < TEST_SIZE; ++ i)
  	{
  		new_entry.int_data = i;
  		INSERT_ENTRY(tuple.data_list, data_t, 0, new_entry, error_code);
  		if(error_code != ERROR_CODE_SUCCESS)
  		{
  			printf("something wrong in the  insert datalist in main.c at line %d .\n", __LINE__);
  			return NULL;
  		}
  		new_entry.p_char_data = (char*)malloc(sizeof(char) * 20);
  		strcpy(new_entry.p_char_data, "wenxiong");
  		INSERT_ENTRY(tuple.data_list, data_t, 0, new_entry, error_code);
  		if(error_code != ERROR_CODE_SUCCESS)
  		{
  			printf("something wrong in the  insert datalist in main.c at line %d .\n", __LINE__);
  			return NULL;
  		}
  
  		rt = insert(p_table, &tuple);
  		if(rt != QUERY_SUCCESS)
  		{
  			printf("insert tuple error.\n");
  		}
  		CLEAR_LIST(tuple.data_list, data_t);
  	}


	// test the add_column function
 	/*p_new_column = (column*)malloc(sizeof(column));
	p_new_column->allow_null = TRUE;
	strcpy(p_new_column->column_name , "TEST");
	p_new_column->is_key = FALSE;
	p_new_column->type = INT_EY;
	p_new_column->i_column_size = 4;

	rt = add_column(p_table, *p_new_column);

	rt = add_column(p_table, *p_new_column);*/

	Error_code e;
	query_return_value	res;
	column aColumn;
	table aTable;
	table* p_table = NULL;
	struct tuple_t* aTuple;
	condition acondition;
	update_struct update_data;
	data_t data;
	int i;

	DECLARE_LISTP(column) p_list_columns;

	DECLARE_LISTP(tuple_t) p_result_set;


	/*strcpy(aTable.name, "student");
	aTable.tuple_count = 0;
	aTable.root		   = bnode_factory_create();

	aTable.column_property = MALLOC_A_LIST(column);*/

	p_list_columns = MALLOC_A_LIST(column);

	INIT_LISTP(p_list_columns);

	strcpy(aColumn.column_name, "studentID");
	aColumn.type = STRING_EY;
	aColumn.i_column_size = 20;
	aColumn.is_key = TRUE;
	aColumn.allow_null = FALSE;
	INSERTP(p_list_columns, column, 0, aColumn, e);

	strcpy(aColumn.column_name, "studentName");
	aColumn.type = STRING_EY;
	aColumn.i_column_size = 40;
	aColumn.is_key = FALSE;
	aColumn.allow_null = TRUE;
	INSERTP(p_list_columns, column, 1, aColumn, e);


	create_table ( &p_table, "student", p_list_columns);

	aTuple = (tuple_t*)malloc(sizeof(tuple_t));

	INIT_LIST(aTuple->data_list);

	for(i = 0; i < 10; i++)
	{
		data.p_char_data = (char*)malloc(40);
		strcpy(data.p_char_data, "082981");
		INSERT_ENTRY(aTuple->data_list, data_t, 0, data, e);

		data.p_char_data = (char*)malloc(40);
		strcpy(data.p_char_data, "shiwen");
		INSERT_ENTRY(aTuple->data_list, data_t, 1, data, e);

		insert(p_table, aTuple);
		CLEAR_LIST(aTuple->data_list, data_t);
	}

	p_result_set = MALLOC_A_LIST(tuple_t);
	INIT_LISTP(p_result_set);


	acondition.com_operator = (compare_operator*)malloc(sizeof(compare_operator)*2);
	acondition.com_operator[1] = acondition.com_operator[0] = EQUAL;

	acondition.com_type = AND;

	acondition.i_condition_count = 2;

	acondition.pp_data_array = (void**)malloc(sizeof(void*) * 2);
	acondition.pp_data_array[0] = malloc(sizeof(char) * 20);
	strcpy((char*)acondition.pp_data_array[0], "082981");
	acondition.pp_data_array[1] = malloc(sizeof(char) * 40);
	strcpy((char*)acondition.pp_data_array[1], "shiwen");

	acondition.p_column_index_array = (int*)malloc(sizeof(int) * 2);
	acondition.p_column_index_array[0] = 0;
	acondition.p_column_index_array[1] = 1;


	res = search_tuple(p_result_set, p_table, &acondition);


	update_data.com_operator = (compare_operator*)malloc(sizeof(compare_operator)*2);
	update_data.com_operator[1] = update_data.com_operator[0] = EQUAL;

	update_data.com_type = AND;

	update_data.i_col_count = 2;

	update_data.pp_data_array = (void**)malloc(sizeof(void*) * 2);
	update_data.pp_data_array[0] = malloc(sizeof(char) * 20);
	strcpy((char*)update_data.pp_data_array[0], "082974");
	update_data.pp_data_array[1] = malloc(sizeof(char) * 40);
	strcpy((char*)update_data.pp_data_array[1], "qiyun");

	update_data.p_column_index_array = (int*)malloc(sizeof(int) * 2);
	update_data.p_column_index_array[0] = 0;
	update_data.p_column_index_array[1] = 1;

	res = update_tuple (p_table, &update_data, &acondition);

	res = delete_tuple(p_table, &update_data);


}
#endif